from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support.select import Select

class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver = driver
    # 封装网址
    def open_url(self,url):
        self.driver.get(url)
    # 时间
    def time_error(self,name):
        t = time.strftime('%Y%m%d%H%M%S')
        self.driver.save_screenshot('../Png/'+t+name)
    # 封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.time_error('.png')
            print(e)
    # 封装输入操作
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    # 封装点击操作
    def click(self,loc):
        self.find_element(loc).click()
    # 封装页面切换
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 封装frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 滚动事件
    def scroll_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    # 滚动到底部
    def scroll_Height(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    # 下拉菜单 根据下标
    def select_index(self,loc,index):
        xb = Select(self.find_element(loc))
        xb.select_by_index(index)
    # 根据value值
    def select_value(self,loc,value):
        xb = Select(self.find_element(loc))
        xb.select_by_value(value)
    # 根据文本
    def select_text(self,loc,text):
        xb = Select(self.find_element(loc))
        xb.select_by_visible_text(text)





